package libs

import "github.com/gin-gonic/gin"

type MiddleWareUser struct {
	Username string `json:"username" binding:"required"`
	Password string `json:"password" binding:"required"`
}

func First(ctx *gin.Context) {
	var middlewareUser MiddleWareUser
	err := ctx.ShouldBindJSON(&middlewareUser)
	if err != nil {
		ctx.String(201, "错误错误！~")
		ctx.Abort() // 阻隔
	}
	ctx.Set("User", middlewareUser)
	ctx.Next()

	ctx.String(200, "验证器对你响应")

}

func Second(ctx *gin.Context) {
	ctx.String(200, "请求中稍后...\r\n")
	user, _ := ctx.Get("User")
	user_instance, ok := user.(MiddleWareUser)
	if ok {
		if len(user_instance.Password) > 4 {
			user_instance.Password = "****" + user_instance.Password[len(user_instance.Password)-5:]
		} else {
			user_instance.Password = "****"
		}
	}

	ctx.JSON(200, user_instance)

	ctx.Next()

	ctx.String(200, "我给你服务~!\r\n")
}

func Last(ctx *gin.Context) {
	ctx.String(200, "再见之前再看一眼...\r\n")
	ctx.Next()

	ctx.String(200, "再见\r\n")
}
